/**
 * 生成枚举列表
 * @param param {object}
 * @param paramMap {object}
 * @example
 * const param = {
 *  high: '1',
 * middle: '2',
 * lower: '3'
 * }
 * cosnt paramMap = {
 * '1': '高',
 * '2': '中',
 * '3': '低',
 * }
 */

import { ElOption, ElSelect } from "element-plus"
import { PropType } from "vue"

interface EnumListDTO {
  label: string
  value: string
}
export function createEnumList<
  T extends Record<string, string>,
  U extends Record<string, string>
>(param: T, paramMap: U): EnumListDTO[] {
  const arr: EnumListDTO[] = []
  Object.entries(param).forEach(([key, value]) => {
    arr.push({
      label: paramMap[value as string] as string,
      value,
    })
  })
  return arr
}

export default defineComponent({
  name: "SEnum",
  props: {
    enumCode: {
      type: Object,
      default: () => ({}),
    },
    enumMap: {
      type: Object,
      default: () => ({}),
    },
  },
  setup(props) {
    return () => (
      <ElSelect>
        {createEnumList(props.enumCode, props.enumMap).map((item) => (
          <ElOption key={item.value} value={item.value} label={item.label} />
        ))}
      </ElSelect>
    )
  },
})
